package com.seven.monior.AppInfo;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.ActivityManager;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;

import com.seven.monior.TaskInfo.TaskInfo;

public class Global {
	public static final String ACTION_UPDATE_UI = "com.seven.monitor.ACTION_UPDATE_UI";
	public static final String ACTION_RESET = "com.seven.monitor.ACTION_RESET";
	public static final String ACTION_UPDATE_TASKLIST = "com.seven.monitor.ACTION_UPDATE_TASKLIST";
	public static final String ACTION_INSERT = "com.seven.monitor.ACTION_INSERT_NOTIFICATION";

	public static final String PREFS = "SettingInfo";
	public static final String VER = "1.0.4 Bulid 20110324";

	public static Date SINCE_TIMESTAMP;
	public static long Total_Mobile_Rx = 0L;
	public static long Total_Mobile_Tx = 0L;
	public static long last_Mobile_Rx = 0L;
	public static long last_Mobile_Tx = 0L;
	public static long Total_Mobile_Rp = 0L;
	public static long Total_Mobile_Tp = 0L;
	public static long last_Mobile_Rp = 0L;
	public static long last_Mobile_Tp = 0L;

	public static long Total_All_Rx = 0L;
	public static long Total_All_Tx = 0L;
	public static long last_All_Rx = 0L;
	public static long last_All_Tx = 0L;
	public static long Total_All_Rp = 0L;
	public static long Total_All_Tp = 0L;
	public static long last_All_Rp = 0L;
	public static long last_All_Tp = 0L;

	public static PackageManager pm;
	public static ActivityManager am;
	public static ConnectivityManager cm;
	public static TelephonyManager mTelephonyMgr;

	public static AppInfo appInfo = null;
	public static ArrayList<TaskInfo> taskInfoList = new ArrayList<TaskInfo>();

	public static ProgressDialog progressDialog;

	public static void resetMobileBytes() {
		if (taskInfoList == null)
			return;

		for (int i = 0; i < taskInfoList.size(); i++) {
			TaskInfo tInfo = taskInfoList.get(i);
			tInfo.m_lMobile_Rx = 0L;
			tInfo.m_lMobile_Tx = 0L;
			tInfo.m_lTotal_Rx = 0L;
			tInfo.m_lTotal_Tx = 0L;
			tInfo.setTotalCpuTime(0);
		}
	}

	public static ArrayList<TaskInfo> getTaskInfos(boolean bGetSystemAppOnly,
			boolean bWithZeroItem, SortType sortType) {
		ArrayList<TaskInfo> fullList = taskInfoList;
		ArrayList<TaskInfo> list = new ArrayList<TaskInfo>();
		for (TaskInfo tInfo : fullList) {
			if (bGetSystemAppOnly) {
				if (!tInfo.isSystemApp())
					continue;
			} else {
				if (tInfo.isSystemApp())
					continue;
			}
			if (!bWithZeroItem) {
				if ((sortType == SortType.Sort_By_Bytes || sortType == SortType.Sort_By_Name)
						&& tInfo.getMobile_RxandTx() <= 0)
					continue;
				else if (sortType == SortType.Sort_By_Cputime
						&& tInfo.getTotalCpuTime() <= 0)
					continue;
			}

			list.add(tInfo);
		}

		SortList(list, sortType);

		return list;
	}

	public static void SortList(ArrayList<TaskInfo> list, SortType sortType) {
		switch (sortType) {
		case Sort_By_Bytes:
			Collections.sort(list, sortByBytes);
			break;
		case Sort_By_Name:
			Collections.sort(list, sortByName);
			break;
		case Sort_By_Cputime:
			Collections.sort(list, sortByCputime);
			break;
		default:
			Collections.sort(list, sortByBytes);
			break;
		}
	}

	public static Comparator<TaskInfo> sortByBytes = new Comparator<TaskInfo>() {

		@Override
		public int compare(TaskInfo rInfo1, TaskInfo rInfo2) {
			// TODO Auto-generated method stub
			Long byte1 = rInfo1.getMobile_RxandTx();
			Long byte2 = rInfo2.getMobile_RxandTx();

			return byte2.compareTo(byte1);
		}
	};

	public static Comparator<TaskInfo> sortByName = new Comparator<TaskInfo>() {

		@Override
		public int compare(TaskInfo rInfo1, TaskInfo rInfo2) {
			// TODO Auto-generated method stub
			String name1 = rInfo1.getApplicationLabel();
			String name2 = rInfo2.getApplicationLabel();

			return name1.compareTo(name2);
		}
	};

	public static Comparator<TaskInfo> sortByCputime = new Comparator<TaskInfo>() {

		@Override
		public int compare(TaskInfo rInfo1, TaskInfo rInfo2) {
			// TODO Auto-generated method stub
			Double time1 = rInfo1.getTotalCpuTime();
			Double time2 = rInfo2.getTotalCpuTime();

			return time2.compareTo(time1);
		}
	};
}
